package com.android.djlmapp.fragment;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.constraint.ConstraintLayout;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.djlmapp.R;
import com.android.djlmapp.adapter.order.OrderPagerAdapter;
import com.android.djlmapp.application.MyApplication;
import com.android.djlmapp.base.BaseFragment;
import com.astuetz.PagerSlidingTabStrip;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

import static com.android.djlmapp.fragment.ChildOrderFragment.BROADBAND_ORDER_TYPE;
import static com.android.djlmapp.fragment.ChildOrderFragment.KAIKA_ORDER_TYPE;
import static com.android.djlmapp.fragment.ChildOrderFragment.ORDER_TYPE;

public class DingDanLieBiaoFragment extends BaseFragment {

    @BindView(R.id.order_fragment_contentLayout)
    ViewPager mViewPager;
    @BindView(R.id.order_fragment_tab_titleLayout)
    PagerSlidingTabStrip mTabLayout;
    @BindView(R.id.iv_top_kf)
    ImageView ivTopKf;
    Unbinder unbinder;
    @BindView(R.id.top_bac)
    ImageView topBac;
    @BindView(R.id.ll_top_back)
    LinearLayout llTopBack;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.home_iv_readpoint)
    ImageView homeIvReadpoint;
    @BindView(R.id.top_notes)
    ImageView topNotes;
    @BindView(R.id.iv_top_msg)
    ConstraintLayout ivTopMsg;
    @BindView(R.id.iv_top_set)
    ImageView ivTopSet;
    @BindView(R.id.tv_top_jump)
    TextView tvTopJump;
    @BindView(R.id.page_title_layout)
    ConstraintLayout pageTitleLayout;
    @BindView(R.id.title_line)
    View titleLine;
    @BindView(R.id.order_fragment_search_et)
    EditText mSearchEdit;
    public static DingDanLieBiaoFragment newInstance(String tabName) {
        DingDanLieBiaoFragment dingDanLieBiaoFragment = new DingDanLieBiaoFragment();
        dingDanLieBiaoFragment.tag = tabName;
        return dingDanLieBiaoFragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        fragmentView = inflater.inflate(R.layout.fragment_order, container, false);
        unbinder = ButterKnife.bind(this, fragmentView);
        llTopBack.setVisibility(View.GONE);
        ivTopMsg.setVisibility(View.GONE);
        ivTopSet.setVisibility(View.GONE);
        titleLine.setVisibility(View.GONE);
        ivTopKf.setVisibility(View.VISIBLE);
        tvName.setText("订单");
        return fragmentView;
    }


    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        super.onLazyInitView(savedInstanceState);
        initView();
    }


    private int mOrderTypeCode = ORDER_TYPE[0];
    /**
     * 获取搜索框对应的提示值
     *
     * @return
     */
    private String getSearchHintInfo() {
        String hintText;
        if (mOrderTypeCode >= KAIKA_ORDER_TYPE && mOrderTypeCode <= BROADBAND_ORDER_TYPE) {
            hintText = "搜索手机号或者订单号";
        } else {
            hintText = "搜索订单号";
        }
        return hintText;
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if(hidden){
            clearSearchEd();
        }
    }

    /**
     * 清空搜索框
     */
    public void clearSearchEd(){
        mSearchContent = null;
        mSearchEdit.setText(mSearchContent);
        mSearchEdit.setHint(getSearchHintInfo());
    }
    /**
     * 搜索内容
     */
    private String mSearchContent;
    @Override
    public void initView() {

        mSearchEdit.setHint(getSearchHintInfo());
        // 搜索订单数据
        mSearchEdit.setOnEditorActionListener((v, actionId, event) -> {
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                mSearchContent = mSearchEdit.getText().toString().trim();
                InputMethodManager im = (InputMethodManager) MyApplication.getInstance().getApplication().getSystemService(Context.INPUT_METHOD_SERVICE);
                if (im != null) {
                    im.hideSoftInputFromWindow(mSearchEdit.getWindowToken(), 0);
                }
                mChildOrderFragment.searchOrder(mSearchContent);
            }
            return false;
        });
        OrderPagerAdapter orderPagerAdapter = new OrderPagerAdapter(getChildFragmentManager());
        mViewPager.setAdapter(orderPagerAdapter);
        mViewPager.setOffscreenPageLimit(orderPagerAdapter.getCount());
        mTabLayout.setDividerColorResource(R.color.trans);
        mTabLayout.setViewPager(mViewPager);
        mChildOrderFragment = (ChildOrderFragment) orderPagerAdapter.getItem(0);
        mViewPager.setCurrentItem(0);
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                mChildOrderFragment = (ChildOrderFragment) orderPagerAdapter.getItem(position);
                mChildOrderFragment.onHiddenChanged(false);
                mOrderTypeCode = ORDER_TYPE[position];
                clearSearchEd();
                mSearchEdit.setHint(getSearchHintInfo());
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                InputMethodManager imm =
                        (InputMethodManager) _mActivity.getSystemService(Activity.INPUT_METHOD_SERVICE);
                if (imm == null) {
                    return;
                }
                View view = _mActivity.getCurrentFocus();
                if (view == null) {
                    view = new View(getContext());
                }
                imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
            }
        });
    }

    private ChildOrderFragment mChildOrderFragment;
    @Override
    public void initData() {

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
